//核心数据
//权限列表部分
//三层for循环思路     最外层不需要判断是否存在值 直接for显示    数据进行传递给下一级  下级菜单判断nodes值是否存在 if判断nodes值进行显示
$(function () {
    authorities = $.session.get('authorities');
    console.log(authorities);
    //ajax start
    $.ajax({
        url: baseURL + "/auth/api/permission/permissionRoleList",
        data: {},
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        success: function (DATA) {
            let yy = DATA.content[0];
            //yy是最外层数据
            //for a 循环 start
            for (let a = 0; a < yy.length; a++) {
                $("#data-list").append(
                    '<div mark="权限组名"  class="QX_group"><i data-toggle="collapse" href="#perm_name' + yy[a].id + '" class="fa fa-chevron-down"></i><span>' + yy[a].text + ' ID :' + yy[a].id + '</span><i class="fa fa-edit fr"></i></div>' +
                    '<div mark="存放子数据" class="collapse title_perm" id="perm_name' + yy[a].id + '"></div>'
                );
                //xx继承yy的数据 是yy下单条组名内的子组名或权限值
                let xx = yy[a].nodes
                //xxs判断xx是否存在nodes 不存在就添加null
                xxs = yy[a].nodes ? yy[a].nodes : null
                //if 判断 start
                if (xxs !== null) {
                    //for b 循环 start
                    //第二次循环
                    for (let b = 0; b < xxs.length; b++) {
                        $("#perm_name" + yy[a].id).append(
                            '<div mark="子权限组名"  class="ZQX_group"><i data-toggle="collapse" href="#perm_name' + xx[b].id + '" class="fa fa-chevron-down"></i><span>' + xx[b].text + '  ID: ' + xx[b].id + '</span><i class="fa fa-edit fr"></i></div>' +
                            '<div mark="存放子数据" class="collapse title_sed_perm" id="perm_name' + xx[b].id + '"></div>'
                        );
                        //    zz继承xx内的nodes数据 最后一级 只能是权限值
                        let zz = xx[b].nodes
                        //zzs判断xx是否存在nodes 不存在就添加null
                        zzs = xx[b].nodes ? xx[b].nodes : null
                        //if 判断 start
                        if (zzs !== null) {
                            //for c 循环 start
                            for (let c = 0; c < zzs.length; c++) {
                                $("#perm_name" + xx[b].id).append(
                                    '<div mark="子权限值" class="QX_data" ><span><i class="fa fa-trash-o"></i><label><input id="' + zz[c].id + '"  name="chkIds" type="checkbox"></label><span class="QX_data_dus">' + zz[c].text + ' id ' + zz[c].id + '</span></span><i class="fa fa-edit fr" data-toggle="modal" href="#edit"></i></div>'
                                );
                                //最内层for结束
                            }
                            //if判断结束
                        }
                        //中间层for结束
                    }
                    //if判断结束
                }
                //最外层for循环
            }
        }
    })
//    ajax end
    $("#data-list").on("click", ".modify-modal", function () {
        $("#sn-modify").text($(this).parents('td').siblings(".sn").text())
        $("#yw-modify").text($(this).parents('td').siblings(".title").text())
        $("#yy-modify").text($(this).parents('td').siblings(".grade").text())
        $("#zz-modify").text($(this).parents('td').siblings(".sn").text())
        $("#zw-modify").text($(this).parents('td').siblings(".sn").text())
        $("#gg-modify").text($(this).parents('td').siblings(".sn").text())
    })
//图标切换
    $('#data-list').on("click", '.fa-chevron-down', function () {
        $(this).removeClass('fa-chevron-down', 'down').addClass('fa-chevron-up', 'up')
    })
    $('#data-list').on("click", '.fa-chevron-up', function () {
        $(this).removeClass('fa-chevron-up').addClass('fa-chevron-down', 'down')
    })
//角色列表删改
//fa_time 删除角色事件
    $("#role_list").on("click", ".fa-times", function () {
        let fap_role_id_arr = $(this).parents('div').attr('id').split('@')
        let fap_role_id = fap_role_id_arr[1]
        console.log(fap_role_id);
        if (confirm("确定删除该权限?")) {
            //点击确定后操作
            $.ajax({
                url: baseURL + '/admin/api/role/roleDel',
                type: 'post',
                dataType: 'json',
                headers: {'authorities': authorities},
                data: {
                    id: fap_role_id,
                },
                success: function (e) {
                    console.log(e);
                    //if 判断 start
                    if (e.code == 1) {
                        console.log("删除角色成功");
                    } else if (e.code != 1) {
                        console.log('创建角色失败');
                    }
                }

            })

        }
        //ajax start
    });
})

//权限角色数据
//jQ start
var role_user_byrole = []
$(function () {
    //ajax start
    $.ajax({
        url: baseURL + '/admin/api/role/getUserByRole',
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        data: {},
        success: function (e) {
            data_list = e.content
            //for i 循环 start
            for (let i = 0; i < data_list.length; i++) {
                $("#role_list").append(
                    '<div id="role_list_id@' + data_list[i].id + '"  class="list-group-item">' + data_list[i].role + '<i class="fa fa-times fr role_list_fa"style="margin-left: 10px;"></i><i data-toggle="modal" href="#edit" class="fa fa-edit fr role_list_fa"></i></div>'
                );
                let data = {}
                data.tid = data_list[i].id
                data.nid = 'role_list_id@' + data_list[i].id
                role_user_byrole = role_user_byrole.concat(data)
            }


        }

    })
    let fap_role_id_arr
    //权限角色分组切换
    $("#role_list").on("click", ".list-group-item", function () {
        $(this).siblings().removeClass("active")
        $(this).addClass("active")
        role_list = $(this).attr("id")
        role_list_tid = find_arr(role_user_byrole, 'nid', role_list, 'tid')
        console.log(role_list_tid);
        $("#user_role_id_readonly").val(role_list_tid);
        fap_role_id_arr = role_list_tid
    });
    //
    $("#save_edit").on("click", function () {

        let user_role_des = $("#user_role_des").val();
        let user_role_name = $("#user_role_name").val();
        //ajax start
        $.ajax({
            url: baseURL + '/auth/api/role/roleEdit',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                id: fap_role_id_arr,
                roleName: user_role_name,
                roleDesc: user_role_des,
            },
            success: function (e) {
                //if 判断 start
                if (e.code != 1) {
                    alert("修改角色信息失败")
                } else {
                    window.location.reload()
                }
            }

        })
    });
})

//添加权限部分
$(function () {
    $("#save_role").click(function () {
        let role_name = $("#role_name").val()
        let role_icon = $("#role_icon").val()
        let role_permission = $("#role_permission").val()
        let role_url = $("#role_url").val()
        let role_pid = $("#role_pid").val()
        let role_sn = $("#role_sn").val()
        let role_label = $("#role_label").val()
        //ajax start
        $.ajax({
            url: baseURL + '/auth/api/permission/permissionAdd',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                name: role_name,
                icon: role_icon,
                permission: role_permission,
                url: role_url,
                pid: role_pid,
                sn: role_sn,
                isMenu: role_label,
            },
            success: function (e) {
                alert("权限添加成功")
            }

        })


    });


})


//jQ start 关系对应部分
$(function () {
    var data = {}
    let data_list_kyeval = "";
    //获取选中了哪些复选框 根据复选框的id生成键值对
    $('body').on('click', 'input[type=checkbox]', function () {
        data[$(this).attr("id")] = $(this).attr("id")
        //if 判断 start
        if ($(this).prop('checked') == true) {
        } else if ($(this).prop('checked') == false) {
            if (!!data[$(this).attr("id")]) {
                delete (data[$(this).attr("id")]);
            }
        }
        console.log(data);
        var data_length = Object.keys(data)
        data_length.length
        //for i 循环 start
        let radem = ""
        for (let i = 0; i < data_length.length; i++) {

            //if 判断 start
            if (data_list_kyeval == "") {
                radem = Object.keys(data)[i]
            } else {
                radem += Object.keys(data)[i] + ","
            }
            data_list_kyeval = radem
        }
        console.log(data_list_kyeval);
    })
//    保存权限关系
    $("#save_primary").click(function () {
        //ajax start
        $.ajax({
            url: baseURL + '/admin/api/role/saveRolePermission',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                roleId: role_list_tid,
                perIds: data_list_kyeval,

            },
            success: function (e) {
                console.log(e);
            }

        })
    });

//find_arr方法 arr目标数组 key等于val时 返回target_key
    function find_arr(arr, key, val, target_key) {
        //for i 循环 start
        for (let i = 0; i < arr.length; i++) {
            //if 判断 start
            if (arr[i][key] == val) {
                return arr[i][target_key]
            }
        }
        return -1
    }

//    新建角色
    $("#save_role_user_modify").click(function () {
        let role_user_name_modify = $("#role_user_name_modify").val();
        let role_user_des_modify = $("#role_user_des_modify").val();
        //ajax start
        $.ajax({
            url: baseURL + '/admin/api/role/roleAdd',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                roleName: role_user_name_modify,
                roleDesc: role_user_des_modify,
            },
            success: function (e) {
                console.log(e);
                //if 判断 start
                if (e.code == 1) {
                    window.location.reload()
                } else if (e.code != 1) {
                    console.log('创建角色失败');
                }
            }

        })
    });


})

//修改权限部分
//jQ start
$(function () {


})